package com.cl.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cl.annotation.IgnoreAuth;
import com.cl.entity.NewsEntity;
import com.cl.entity.view.NewsView;
import com.cl.service.NewsService;
import com.cl.utils.*;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 获取分页数据（后端）
     */
    @RequestMapping("/page")
    public R getPage(@RequestParam Map<String, Object> params, NewsEntity news, HttpServletRequest request) {
        EntityWrapper<NewsEntity> wrapper = createEntityWrapper(news);
        PageUtils page = newsService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(wrapper, news), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 获取分页数据（前端）
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R getList(@RequestParam Map<String, Object> params, NewsEntity news, HttpServletRequest request) {
        EntityWrapper<NewsEntity> wrapper = createEntityWrapper(news);
        PageUtils page = newsService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(wrapper, news), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 获取列表数据
     */
    @RequestMapping("/lists")
    public R getLists(NewsEntity news) {
        EntityWrapper<NewsEntity> wrapper = createEntityWrapper(news);
        wrapper.allEq(MPUtil.allEQMapPre(news, "news"));
        return R.ok().put("data", newsService.selectListView(wrapper));
    }

    /**
     * 查询单条数据
     */
    @RequestMapping("/query")
    public R query(NewsEntity news) {
        EntityWrapper<NewsEntity> wrapper = createEntityWrapper(news);
        wrapper.allEq(MPUtil.allEQMapPre(news, "news"));
        NewsView newsView = newsService.selectView(wrapper);
        return R.ok("查询公告信息成功").put("data", newsView);
    }

    /**
     * 获取详情（后端）
     */
    @RequestMapping("/info/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        NewsEntity news = newsService.selectById(id);
        news = newsService.selectView(new EntityWrapper<NewsEntity>().eq("id", id));
        return R.ok().put("data", news);
    }

    /**
     * 获取详情（前端）
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R getDetail(@PathVariable("id") Long id) {
        NewsEntity news = newsService.selectById(id);
        news = newsService.selectView(new EntityWrapper<NewsEntity>().eq("id", id));
        return R.ok().put("data", news);
    }

    /**
     * 保存数据（后端）
     */
    @RequestMapping("/save")
    public R save(@RequestBody NewsEntity news) {
        prepareEntityForSave(news);
        newsService.insert(news);
        return R.ok();
    }

    /**
     * 保存数据（前端）
     */
    @RequestMapping("/add")
    public R add(@RequestBody NewsEntity news) {
        prepareEntityForSave(news);
        newsService.insert(news);
        return R.ok();
    }

    /**
     * 更新数据
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody NewsEntity news) {
        newsService.updateById(news);
        return R.ok();
    }

    /**
     * 删除数据
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        newsService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 创建通用的 EntityWrapper
     */
    private EntityWrapper<NewsEntity> createEntityWrapper(NewsEntity news) {
        return new EntityWrapper<>();
    }

    /**
     * 准备实体数据用于保存
     */
    private void prepareEntityForSave(NewsEntity news) {
        news.setId(new Date().getTime() + (long) (Math.random() * 1000));
    }
}